package com.doit.demo05;

@SuppressWarnings("all")
public class tickt implements Runnable {
    private int i=100;
    private Object obj = new Object();

    @Override
    public void run() {
        sell();
//        sell2();
        //sell2是对sell的解释
    }

    private void /*static*/ sell2() {
        while(true) {
            synchronized (this) { //如果是static类型的,那么对象就不能写this或者super 只能写当前对象名.class
                if (i > 0) {
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "正在卖票" + i);
                    i--;
                }
                else
                    System.exit(0);
            }
        }
    }

    private synchronized void sell()
    {
        while(true)
        {
            if (i > 0) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "正在卖票" + i);
                i--;
            }
            else
                System.exit(0);
        }
    }
}
